<template>
  <div class="app-container">
    <jy-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="this.subTitle + '人'" prop="personnel">
              <el-input v-model="form.personnelName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="this.subTitle + '时间'" prop="date">
              <el-date-picker clearable v-model="form.date" type="date" value-format="yyyy-MM-dd"
                :placeholder="'请选择' + this.subTitle + '时间'" style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item :label="this.subTitle + '内容'" prop="content">
          <el-input type="textarea" v-model="form.content" :placeholder="'请输入' + this.subTitle + '内容'" />
        </el-form-item>
        <!-- <el-form-item v-if="this.form.type == 2" label="结果" prop="outcome">
          <el-input v-model="form.outcome" :placeholder="'请输入' + this.subTitle + '结果'" />
        </el-form-item> -->
        <el-form-item label="附件" prop="fileId">
          <fileUpload v-model="form.fileId" :oldList="form.fileIdOld" :fileType="['doc', 'docx', 'xlsx', 'xls'
            , 'pdf', 'png', 'jpg', 'jpeg']" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm('通过')">
          {{ form.type == 1 ? '确 定' : '通过' }}</el-button>
        <el-button v-if="form.type == 2" :loading="buttonLoading" type="danger" @click="submitForm('驳回')">驳回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { addInspectionIssueRecording } from "@/api/scene/inspectionIssueRecording";
import { mapGetters } from "vuex";

export default {
  name: "InspectionIssueRecording",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 弹出层标题
      title: "",
      subTitle: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personnel: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "时间不能为空", trigger: "blur" }
        ],
        outcome: [
          { required: true, message: "结果不能为空", trigger: "blur" }
        ],
        // fileId: [
        //   { required: true, message: "附件不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["userId", "nickName"])
  },
  created () {
  },
  methods: {
    init (inspectionId, type) {
      this.reset();
      this.form.inspectionId = inspectionId;
      //  1: 整改，2: 复查
      this.form.type = type;
      this.open = true;
      this.subTitle = type == 1 ? '整改' : '复查';
      this.title = `添加现场巡查${this.subTitle}记录`;
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        inspectionId: undefined,
        type: undefined,
        personnel: this.userId,
        personnelName: this.nickName,
        content: undefined,
        date: this.parseTime(new Date().getTime(), '{y}-{m}-{d}'),
        outcome: undefined,
        fileId: undefined,
        fileIds: [],
        fileIdOld: [],
        approvalStatus: '22'
      };
      this.resetForm("form");
    },
    handleAdd () {

    },

    /** 提交按钮 */
    submitForm (outcome) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fileId) {
            this.form.fileIds = this.form.fileId.split(',');
          }
          this.form.outcome = outcome;
          if (this.form.type == 1) {
            this.form.approvalStatus = '22';
          } else if (this.form.type == 2) {
            this.form.approvalStatus = outcome == '通过' ? '33' : '11';
          }
          this.buttonLoading = true;
          addInspectionIssueRecording(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.cancel();
            this.$emit("callBack");
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
  }
};
</script>
